//
//  LtqNavgationTitleFlowLayout.m
//  UI15_homework_网易新闻导航条(自适应文字长度)
//
//  Created by Hanlingyu on 16/8/3.
//  Copyright © 2016年 ltq. All rights reserved.
//

#import "LtqNavgationTitleFlowLayout.h"

@interface LtqNavgationTitleFlowLayout ()

@property (nonatomic, strong) NSMutableArray<UICollectionViewLayoutAttributes *>* attributesArray;
@property (nonatomic, assign) CGFloat totalWidth;
@end



@implementation LtqNavgationTitleFlowLayout


- (instancetype)init {
    self = [super init];
    if (self) {
        _attributesArray = [NSMutableArray array];
        _totalWidth = 0.0f;
//        _contentWidth = 0.0f;
        
    }
    return self;
    
}

- (void)prepareLayout {
    
    if (_attributesArray.count != 0) {
        [_attributesArray removeAllObjects];
    }

    for (int i = 0; i < [self.collectionView numberOfItemsInSection:0]; i++) {
        
        NSIndexPath *index = [NSIndexPath indexPathForItem:i inSection:0];

        UICollectionViewLayoutAttributes *layoutAttributes = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:index];
        CGFloat height = 40;
        CGFloat width = [self.delegate collectionView:self.collectionView layout:self widthForItemAtIndexPath:index height:height];
        CGFloat y =  0;
        CGFloat x = _totalWidth;
        _totalWidth = _totalWidth + width;
        NSLog(@"%lf", _totalWidth);
        layoutAttributes.frame = CGRectMake(x, y, width, height);
        [_attributesArray addObject:layoutAttributes];
        
    }
}

- (NSArray<UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect {
    
    return _attributesArray;
}

- (CGSize)collectionViewContentSize {
    
    return CGSizeMake(_totalWidth, 0);
}


@end
